<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    okText="生成模板"
    cancelText="关闭"
  >
    <LabEntrustItemsForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false" />
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineExpose, watch, reactive, onBeforeUpdate } from 'vue';
  import LabEntrustItemsForm from './LabEntrustItemsForm.vue';

  const title = ref<string>('');
  const width = ref<number>(900);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const registerForm = ref();
  const emit = defineEmits(['register', 'success']);
  let itemId = ref('');

  /**
   * 新增
   */
  function add(data) {
    title.value = '新增-实验模板';
    visible.value = true;
    itemId.value = data.itemId;
    nextTick(() => {
      registerForm.value.add(data);
    });
  }
  /**
   * 编辑
   * @param record
   */
  function edit(record) {
    title.value = disableSubmit.value ? '详情-实验模板' : '编辑-实验模板';
    visible.value = true;
    itemId.value = record.itemId;
    nextTick(() => {
      registerForm.value.edit(record);
    });
  }

  //数据更新
  onBeforeUpdate(() => {
    nextTick(() => {
      registerForm.value.reloadTable(itemId.value);
    });
  });
  //监听itemID变化
  watch(itemId, (newValue, oldValue) => {
    nextTick(() => {
      registerForm.value.reloadTable(newValue.itemId);
    });
    // registerForm.value.reloadTable();
  });

  /**
   * 确定按钮点击事件
   */
  function handleOk() {
    registerForm.value.submitForm();
  }

  /**
   * form保存回调事件
   */
  function submitCallback() {
    handleCancel();
    emit('success');
  }

  /**
   * 取消按钮回调事件
   */
  function handleCancel() {
    visible.value = false;
  }

  defineExpose({
    add,
    edit,
    disableSubmit,
  });
</script>

<style>
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }
</style>
